﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Text;
using SWSoft.Framework.Web;
using SIIMWeb.YIBAO;

namespace TradeTestWeb
{
    public partial class editor : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var fileinfo = new FileInfo(new DirectoryInfo(Server.MapPath("~")).Parent.FullName + "\\Tools\\TradeTest\\" + Request["yb"] + "\\" + Request["file"] + ".XML");
                if (fileinfo.Exists)
                {
                    Session["path"] = fileinfo.FullName;
                    var lines = File.ReadAllLines(fileinfo.FullName);
                    var text = "";
                    foreach (var item in lines)
                    {
                        text += item + "\r\n";
                    }
                    textxml.InnerText = ShowXml(File.ReadAllText(fileinfo.FullName));
                    textxml.Rows = lines.Length;
                }
            }
        }

        public void save(string xml)
        {

            File.WriteAllText(Session["path"].ToString(), ShowXml(xml));
        }

        /// <summary>
        /// XML格式化为文本显示
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ShowXml(string str)
        {
            MemoryStream mstream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mstream, null);
            XmlDocument xmldoc = new XmlDocument();
            writer.Formatting = Formatting.Indented;

            xmldoc.LoadXml(str);
            xmldoc.WriteTo(writer);
            writer.Flush();
            writer.Close();

            string strReturn = Encoding.UTF8.GetString(mstream.ToArray());
            mstream.Close();
            return strReturn;
        }
    }
}